#include "dxdiagcom.h"
#include "resources.h"


int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, PSTR pCmdLine, int iCmdShow)
{

    HRESULT hr;
    DXDIAG_INIT_PARAMS params;
    IDxDiagProvider* pddp = NULL;
    IDxDiagContainer* pddc = NULL;
    IDxDiagContainer* system = NULL;
    VARIANT var;
    COM_SystemInfo sys;
    HINSTANCE hInstance = GetModuleHandleW(0);
    WCHAR prop[MAX_STRING_LEN];
    WCHAR container[MAX_STRING_LEN]; // {'D','x','D','i','a','g','_','S','y','s','t','e','m','I','n','f','o'};
    char value[MAX_STRING_LEN];

    LoadStringW(hInstance, COM_SYSTEM_SZMACHINENAMEENGLISH, prop, MAX_STRING_LEN);
    WideCharToMultiByte(CP_UTF8, 0, prop, MAX_STRING_LEN, value, MAX_STRING_LEN, NULL, NULL);
    printf("property name: %s\n", value);

    LoadStringW(hInstance, COM_SYSTEMINFO, container, MAX_STRING_LEN);

    WideCharToMultiByte(CP_UTF8, 0, container, MAX_STRING_LEN, value, MAX_STRING_LEN, NULL, NULL);
    printf("container name: %s\n", value);

    CoInitialize(NULL);
    hr = CoCreateInstance(&CLSID_DxDiagProvider, NULL, CLSCTX_INPROC_SERVER,
                          &IID_IDxDiagProvider, (LPVOID*)&pddp);
    if (FAILED(hr))
        goto failed;

    params.dwSize = sizeof(params);
    params.dwDxDiagHeaderVersion = DXDIAG_DX9_SDK_VERSION;
    params.bAllowWHQLChecks = FALSE;
    params.pReserved = NULL;
    hr = IDxDiagProvider_Initialize(pddp, &params);
    if (FAILED(hr))
        goto failed;

    hr = IDxDiagProvider_GetRootContainer(pddp, &pddc);
    if (FAILED(hr))
        goto failed;

    hr = IDxDiagContainer_GetChildContainer(pddc, container, &system);
    if (FAILED(hr))
        goto failed;

    hr = IDxDiagContainer_GetProp(system, prop, &var);
    if (FAILED(hr))
        goto failed;


    sys.machineName = V_BSTR(&var);
    WideCharToMultiByte(CP_UTF8, 0, sys.machineName, 6, value, 1000, NULL, NULL);
    printf("result: %s\n", value);

    return 0;

failed:
    wprintf(L"Failed");
    return 1;
}
